1433B - Yet Another Bookshelf - CodeForces Solution


greedy implementation *800

Please click on ads to support us..

Python Code:

for _ in range(int(input())) :
    
    n = int(input())
    arr = "".join(list(map(str, input().split())))
    arr = arr.strip("0")
    n = len(arr)
    
    arr0 = ["0"]*n
    
    for i in range(n) :
        if arr[i] == "0" :
            arr0[i] = "1"
        
    arr0 = "".join(arr0).strip("0")
        
    sm = 0    
        
    for i in arr0 :
        sm += int(i)
        
    print(sm if arr.count("1") > 1 else 0)

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
#define For(i, s, e) for (ll i = s; i < e; i++)
#define FOR(i, s, e) for (ll i = s; i <= e; i++)
#define FORD(i, s, e) for (ll i = s; i >= e; i--)
#define pb push_back
#define vii vector<ll>
#define makep make_pair
#define vpll vector<pair<ll, ll>>
#define fi first
#define se second
#define sii set<ll>
#define pii pair<int, int>
#define rev(c) reverse(c.begin(), c.end())
#define sortf(c) sort(c.begin(), c.end())
#define sortd(c) sort(c.begin(), c.end(), greater<int>())
#define test()   \
    int test;    \
    cin >> test; \
    while (test--)
#define fast()                        \
    ios_base::sync_with_stdio(false); \
    cin.tie(0);                       \
    cout.tie(0);
#define sqr(n) (n)*(n)
#define len(s) (int) (s.length())
#define re0 return 0
#define re return

int const max2d = 1e4 + 7;
int const N = 1e6 + 7;
int const inf = 1e12 + 7;
int const mod = 1e9 + 7; 
char const nl = '\n';

// ! ¸,ø¤º°`°º¤ø,¸¸,ø¤º° [ нvмegy ] °º¤ø,¸¸,ø¤º°`°º¤ø,¸ roadтoнυe

signed main()
{
    fast();
    
    test() { 
        int n; 
        int st, ed; 
        st = ed = 0; 
        cin >> n;
        vii a(n+1, 0); 
        FOR(i, 1, n) { 
            cin >> a[i]; 
            if (a[i]) { 
                if (!st) st = i;
                else ed = i;
            }
        }
        int cnt = 0;
        FOR(i, st, ed) { 
            if (!a[i]) cnt++;
        }
        cout << cnt << nl;
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship
Health of a person
Divisibility
A. Movement
Numbers in a matrix
Sequences
Split houses